#include "mychartview.h"


MyChartView::MyChartView(QChart *chart, QWidget *parent)
    : QChartView{chart,parent}
{
    /*指针传递*/
    this->chart = chart;
    this->chart->legend()->hide();
    this->resize(800,400);
}

MyChartView::~MyChartView()
{
  delete chart;
}

void MyChartView::setChartStyle(QChart::AnimationOptions options)
{
   this->chart->setAnimationOptions(options);
}

void MyChartView::closeEvent(QCloseEvent *event)
{
    int ret = QMessageBox::question(this,"确认","是否需要关闭窗口？确认将关闭绘图程序！");
    if(ret == QMessageBox::Yes){
        //关闭串口
        //处理关闭窗口事件
        event->accept();
    }else{
        //不关闭
        event->ignore(); //事件继续传递给父组件
    }
}

void MyChartView::mousePressEvent(QMouseEvent *event)
{

    if(event->button() == Qt::RightButton){
        //qDebug() << "鼠标R";
        /*回到当前最新点的位置*/
        //如何实现？
        //取消缩放
        this->chart->zoomReset();

    }else if(event->button() == Qt::LeftButton){
        //qDebug() << "鼠标L";
        //记录当前
        p = event->pos();
    }
}


void MyChartView::mouseMoveEvent(QMouseEvent *event)
{
    /* 一定要 带S*/
    if(event->buttons() & Qt::LeftButton){
        /*单次只滚动一个轴*/
        if(abs(p.x() - event->x()) > abs(event->y() - p.y())){
            this->chart->scroll((p.x() - event->x())*0.02,0);
        }else{
            this->chart->scroll(0,(event->y() - p.y())*0.002);
        }
    }

}

void MyChartView::wheelEvent(QWheelEvent *event)
{
    /*需不需要 同时更改tick 的数量*/
    if(event->angleDelta().y() > 0){//滚轮上滑动
        this->chart->zoom(1.2);
    }else if(event->angleDelta().y() < 0){ //滚轮下滑时

        this->chart->zoom(0.8);
    }
}

